package com.dongdong.bookstore.module.ams.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.dongdong.bookstore.common.service.RedisService;
import com.dongdong.bookstore.module.ams.model.Article;
import com.dongdong.bookstore.module.ams.service.AmsCacheService;
import com.dongdong.bookstore.module.ams.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 林泉梁
 * @version 1.0
 */
@Service
@Slf4j
public class AmsCacheServiceImpl implements AmsCacheService {

    @Value("${redis.database}:${redis.key.ams.liek}")
    private String prefixLike;
    @Value("${redis.database}:${redis.key.ams.view}")
    private String prefixView;

    @Autowired
    private RedisService redisService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public void delLike(Long articleId) {
        Article article = articleService.getArticleById(articleId);
        if (article != null) {
            String key = prefixLike + ":" + articleId;
            redisService.del(key);
        }

    }

    @Override
    public int getLike(Long articleId) {
        String key = prefixLike + ":" + articleId;
        Set<Object> objects = redisService.sMembers(key);
        if (CollUtil.isEmpty(objects)) {
            return 0;
        }
        return objects.size();
    }

    @Override
    public boolean judgetLike(Long articleId, Long userId) {
        String key = prefixLike + ":" + articleId;
        Boolean isExist = redisService.sIsMember(key, userId);
        if (isExist) {
            redisService.sRemove(key, userId);
            return false;
        } else {
            redisService.sAdd(key, userId);
            return true;
        }
    }

    @Override
    public boolean ifLike(Long articleId, Long userId) {
        String key = prefixLike + ":" + articleId;
        return redisService.sIsMember(key, userId);
    }

/*    @Override
    public void setViews(Long articleId) {
        String key = prefixView + ":" + articleId;
        redisService.set(key, 0);

    }*/

    @Override
    public void delViews(Long articleId) {
        String key = prefixView + ":" + articleId;
        redisService.del(key);
    }

    @Override
    public int getViews(Long articleId) {
        String key = prefixView + ":" + articleId;
        Object o = redisService.get(key);
        if (o == null) {
            return 0;
        }

        return (int) o;
    }

    @Override
    public void addView(Long articleId) {
        String key = prefixView + ":" + articleId;
/*        Object o = redisService.get(key);
        if (o == null) {
            setViews(articleId);
        }*/
        redisService.incr(key, 1);
    }
}
